﻿using STHPMS1.BLL.B_Core;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq.Expressions;
using System.Linq;

namespace STHPMS1.BLL.B_Bus
{
    public class Dr_InitialconstructioncostBus : BllBase, IDr_InitialconstructioncostBill
    {
        public void Save<T>(T entity)
            where T : class ,IDr_Initialconstruction
        {
            if (this.Scene.Dal.Sthpms1.Update(entity) == 0)
            {
                this.Scene.Dal.Sthpms1.Insert(entity);
            }
        }

        public void Remove(IDr_Initialconstruction entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IDr_Initialconstruction
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info);
        }

        public dynamic GetIniCost(string proj_Id,string mainsegid )
        {
            dynamic data=new ExpandoObject();
            data.planList= new ArrayList();
            dynamic plan=new ExpandoObject();
            plan.Iniproj_Id = null;

            DataTable table=this.Scene.Dal.Sthpms1.Dr_Initialconstructioncost.GetIniCost(proj_Id);
            List<Dr_Stru_Mate> stru_MateList = this.Scene.Dal.Sthpms1.Select<Dr_Stru_Mate>();
            if (stru_MateList.Count == 0) throw new Exception("大中修材料库的内容为空!");
            Dn_Divi_Result mainSeg=this.Scene.Dal.Sthpms1.Select<Dn_Divi_Result>(p=>p.Mainsegid==mainsegid).FirstOrDefault();
            if (mainSeg==null) throw new Exception("路段不存在 - "+mainsegid);


            foreach (DataRow dr in table.Rows)
	        {
                if(plan.Iniproj_Id!=dr["INIPROJ_ID"].ToString())
                {
                    Dr_Largemeas_Ini ini = this.Scene.Dal.Sthpms1.Select<Dr_Largemeas_Ini>(p => p.Iniproj_Id == dr["INIPROJ_ID"].ToString()).FirstOrDefault();
                    if (ini == null) throw new Exception("大中修初选方案不存在 - " + dr["INIPROJ_ID"].ToString());

                    plan=new ExpandoObject();
                    plan.Iniproj_Id = ini.Iniproj_Id;
                    plan.Meas_Name = ini.Meas_Name;
                    plan.mateList=new ArrayList();
                    plan.Inicons_cost = (double)dr["INICONS_COST"]/10000;
                    data.planList.Add(plan);
                }

                Dr_Stru_Mate m = stru_MateList.FirstOrDefault(p => p.Stru_Id == dr["STRU_ID"].ToString());
                if (m == null) throw new Exception("材料数据不存在 - " + dr["STRU_ID"].ToString());
                dynamic mate = new ExpandoObject();
                mate.Stru_Name = m.Stru_Name;
                mate.Mater_thick = (double)dr["MATER_THICK"];
                mate.Stru_Price = (double)dr["MATER_THICK"] * m.Stru_Price;
                mate.Amount = (double)dr["AMOUNT"];
                mate.Inicons_cost = mate.Stru_Price * mate.Amount * (mainSeg.Endmp - mainSeg.Beginmp) / 10000;//初期建设费用以万元为单位
                plan.mateList.Add(mate);
            }
            
           
            return data;
        }
    }
}
